home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cl_7_bug.zip / BUG_6.C < prev    next >
C/C++ Source or Header  |  1992-10-10  |  3KB  |  96 lines

  1. /* Test function that fails to compile */
  2.  
  3. #include <stdio.h>
  4. #include <malloc.h>
  5.  
  6. #define PC_MALLOC 1024
  7. #define PS_MALLOC 1024
  8. #define ADD_TO_PTR(ptr,size,type) (type) ((long) (ptr)+size)
  9. #define PTR_BYTE_DIFF(A,B) ((long) (A) - (long) (B))
  10. #define MALLOC_OVERHEAD 32
  11.  
  12. typedef unsigned int uint;
  13. typedef char * string;
  14. typedef char byte;
  15. typedef void * gptr;
  16.  
  17. typedef struct st_typelib {
  18.   int count;
  19.   char * *type_names;
  20. } TYPELIB;
  21.  
  22. typedef struct st_pointer_array {        /* when using array-strings */
  23.   TYPELIB typelib;                /* Pointer to strings */
  24.   char    *str;                    /* Strings is here */
  25.   char    *flag;                    /* Flag about each var. */
  26.   int  array_allocs,max_count,length,max_length;
  27. } POINTER_ARRAY;
  28.  
  29.  
  30. int insert_pointer_name(pa,name)
  31. register POINTER_ARRAY *pa;
  32. string name;
  33. {
  34.   int i,length,old_count;
  35.   byte *new_pos,**new_array;
  36.  
  37.   if (! pa->typelib.count)
  38.   {
  39.     if (!(pa->typelib.type_names=(string *)
  40.       malloc(((PC_MALLOC-MALLOC_OVERHEAD)/
  41.              (sizeof(string)+sizeof(*pa->flag))*
  42.              (sizeof(string)+sizeof(*pa->flag))))))
  43.       return(-1);
  44.     if (!(pa->str= (byte*) malloc(PS_MALLOC-MALLOC_OVERHEAD)))
  45.     {
  46.       free((gptr) pa->typelib.type_names);
  47.       return (-1);
  48.     }
  49.     pa->max_count=(PC_MALLOC-MALLOC_OVERHEAD)/(sizeof(byte*)+
  50.                            sizeof(*pa->flag));
  51.     pa->flag= (char*) (pa->typelib.type_names+pa->max_count);
  52.     pa->length=0;
  53.     pa->max_length=PS_MALLOC-MALLOC_OVERHEAD;
  54.     pa->array_allocs=1;
  55.   }
  56.   length=strlen(name)+1;
  57.   if (pa->length+length >= pa->max_length)
  58.   {
  59.     if (!(new_pos= (byte*) realloc((gptr) pa->str,
  60.                       (uint) (pa->max_length+PS_MALLOC))))
  61.       return(1);
  62.     if (new_pos != pa->str)
  63.     {
  64.       long diff= PTR_BYTE_DIFF(new_pos,pa->str);
  65.       for (i=0 ; i < pa->typelib.count ; i++)
  66.     pa->typelib.type_names[i]= ADD_TO_PTR(pa->typelib.type_names[i],diff,
  67.                           char*);
  68.       pa->str=new_pos;
  69.     }
  70.     pa->max_length+=PS_MALLOC;
  71.   }
  72.   if (pa->typelib.count >= pa->max_count-1)
  73.   {
  74.     int len;
  75.     pa->array_allocs++;
  76.     len=(PC_MALLOC*pa->array_allocs - MALLOC_OVERHEAD);
  77.     if (!(new_array=(string*) realloc((gptr) pa->typelib.type_names,
  78.                      (uint) len/
  79.                      (sizeof(byte*)+sizeof(*pa->flag))*
  80.                      (sizeof(byte*)+sizeof(*pa->flag)))))
  81.       return(1);
  82.     pa->typelib.type_names=new_array;
  83.     old_count=pa->max_count;
  84.     pa->max_count=len/(sizeof(byte*) + sizeof(*pa->flag));
  85.     pa->flag= (char*) (pa->typelib.type_names+pa->max_count);
  86.     memcpy((byte*) pa->flag,(string) (pa->typelib.type_names+old_count),
  87.        old_count*sizeof(*pa->flag));
  88.   }
  89.   pa->flag[pa->typelib.count]=0;            /* Reset flag */
  90.   pa->typelib.type_names[pa->typelib.count++]= pa->str+pa->length;
  91.   pa->typelib.type_names[pa->typelib.count]= 0;    /* Put end-mark */
  92.   VOID(strmov(pa->str+pa->length,name));
  93.   pa->length+=length;
  94.   return(0);
  95. } /* insert_pointer_name */
  96.